package io.renren.modules.frt.controller;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.MapUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.frt.entity.EconomicCooperEntity;
import io.renren.modules.frt.entity.EconomicZoneEntity;
import io.renren.modules.frt.entity.SumaryEntity;
import io.renren.modules.frt.service.EconomicCooperService;
import io.renren.modules.frt.service.EconomicZoneService;
import io.renren.modules.frt.service.SumaryService;
import io.renren.modules.sys.controller.AbstractController;



/**
 * 汇总图表
 *
 * @author qibing.huang
 * @email abingtech@163.com
 * @date 2018-12-03 22:42:14
 */
@RestController
@RequestMapping("frt/sumary")
public class SumaryController extends AbstractController {
    @Autowired
    private SumaryService sumaryService;
    @Autowired
    private EconomicZoneService economicZoneService;
    @Autowired
    private EconomicCooperService economicCooperService;

    /**
     * 列表
     */
	@RequestMapping("/list")
    @RequiresPermissions("frt:sumary:list")
    public R list(@RequestParam Map<String, Object> params){
    	params.put("userId", this.getUserId());
    	String taskName = MapUtils.toStr(params, "taskName");
    	if(StringUtils.isNotEmpty(taskName)) {
    		params.put("taskName", "%"+taskName+"%");
    	}
    	String zoneName = MapUtils.toStr(params, "zone_name");
    	List<EconomicZoneEntity> zoneList = null;
    	if(StringUtils.isNotEmpty(zoneName)) {
    		zoneList = economicZoneService.selectByMap(new MapUtils().put("zone_name",zoneName));
    	}
    	String cooperName = MapUtils.toStr(params, "cooper_name");
    	List<EconomicCooperEntity> cooperList = null;
    	if(StringUtils.isNotEmpty(cooperName)) {
    		cooperList = economicCooperService.selectByMap(new MapUtils().put("cooper_name",cooperName));
    	}
    	
    	Set<String> provinceSet = new HashSet<>();
    	if(zoneList != null && zoneList.size() > 0) {
    		for(EconomicZoneEntity zone : zoneList) {
            	provinceSet.add(zone.getProvince());
     		}
    	}
    	if(cooperList != null && cooperList.size() > 0) {
    		for(EconomicCooperEntity cooper : cooperList) {
     			provinceSet.add(cooper.getProvince());
     		}
    	}
    	params.put("provinceSet", provinceSet);
    	
        PageUtils page = sumaryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("frt:sumary:info")
    public R info(@PathVariable("id") Long id){
        SumaryEntity sumary = sumaryService.selectById(id);

        return R.ok().put("sumary", sumary);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("frt:sumary:save")
    public R save(@RequestBody SumaryEntity sumary){
		sumaryService.insert(sumary);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("frt:sumary:update")
    public R update(@RequestBody SumaryEntity sumary){
		sumaryService.updateById(sumary);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("frt:sumary:delete")
    public R delete(@RequestBody Long[] ids){
		sumaryService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
